﻿#region Legal notice
/*
 * This file is part of EPPLib.
 * Copyright 2010, 2011 
 * Gianluca Esposito (info@gianlucaesposito.it)
 * ----------------------------------------------------------------------------
 * EPPLib is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 * 
 * EPPLib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with Nome-Programma; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
#endregion

#region Using directives
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Xml.Serialization;
using System.Text;
using System.Xml;
using System.Xml.Schema;
#endregion

namespace EPPLib.Messages
{
    public abstract class BaseMessage
    {
        protected bool _emptyNamespace = false;

        [XmlIgnore]
        public bool EmptyNamespace { get { return _emptyNamespace; } set { _emptyNamespace = value; } }

        public string Serialize()
        {
            try
            {
                string retStr = null;
                XmlSerializer serializer = new XmlSerializer(this.GetType());
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.OmitXmlDeclaration = true;
                settings.Encoding = new UTF8Encoding(false);
                settings.ConformanceLevel = ConformanceLevel.Document;
                settings.Indent = true;

                MemoryStream ms = new MemoryStream();
                using (XmlWriter writer = XmlWriter.Create(ms, settings))
                {
                    if (_emptyNamespace)
                    {
                        XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
                        namespaces.Add("", "");
                        serializer.Serialize(writer, this, namespaces);
                    }
                    else
                    {
                        XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
                        namespaces.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
                        serializer.Serialize(writer, this, namespaces);
                    }
                }
                retStr = ToString(ms.ToArray());
                return retStr;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public object Deserialize(string xml)
        {
            XmlSerializer xs = new XmlSerializer(this.GetType());
            MemoryStream memoryStream = new MemoryStream(ToByteArray(xml));
            XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
            return xs.Deserialize(memoryStream);
        }

        private string ToString(Byte[] bytes)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            String constructedString = encoding.GetString(bytes);
            return (constructedString);
        }

        private Byte[] ToByteArray(String xml)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            Byte[] byteArray = encoding.GetBytes(xml);
            return byteArray;
        }
    }
}